#!/bin/bash
#----------------- procedures ----------------
displayHelpfile ()
{
  local HELPFILE=$1
  echo "<TABLE WIDTH=60%><TR><TD>"
  cat $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE
  echo "</TD></TR></TABLE>"

}

printUpdateForm ()
{
  local HIGHLIGHT="$1"
  local DESCRIPTION=$2
  echo "Insert text.<BR>
  <TABLE>
  <FORM method=\"post\" Action=$SCRIPT_NAME?help+doUpdateText+$HELPPARAM+$HELPFILE>
  <TR><TD>Highlight</TD><TD><INPUT NAME=\"boldtext\" VALUE=\"${HIGHLIGHT:3:`expr ${#HIGHLIGHT} - 11`}\" Size=60 Type=\"text\"></TD></TR>
  <TR><TD>Text</TD><TD><TEXTAREA NAME=\"helpinfo\" ROWS=12 COLS=60>$DESCRIPTION</TEXTAREA></TD></TR>
  <TR><TD COLSPAN=2 ALIGN=center><INPUT TYPE=submit Value=\"Update helppage\">
  <INPUT TYPE=reset Value=\"CLEAR\"></TD></TR>
  </FORM>
  </TABLE>"
}
# ------------------------------- Main -------------------------------- 
# SIM_HELPDIR=$SIM_MAINDIR/help
# SIM_HELPLANG=en
# SIM_HELPTOOL$SIM_PLUGINSDIR/helptool3.sh
HELPACTION=$1
HELPPARAM=$2
ETOOL=$3
EACTION=$4
PARAM1=$5
echo "HELPACTION: $HELPACTION, HELPPARAM: $HELPPARAM, ETOOL: $ETOOL, EACTION: $EACTION PARAM1: $PARAM1.<HR>"

if [ "$EACTION" != "none" ]; then
  HELPFILE="$ETOOL+$EACTION" 
else
  HELPFILE="$ETOOL+"
fi
  echo "Working on $HELPFILE."
# determine per tool which and howmany parameters are to beconsidderd to make
# a task unique.
  echo "
  <A HREF=$SCRIPT_NAME?help+display+file+help+about>About</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+howto>Howto</A>
  "
if [ "$USERLEVEL" -gt "7" ]; then
  echo "-
  <A HREF=$SCRIPT_NAME?help+display+file+help+nms>Nms</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+changes>Changes</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+firewall>Firewall</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+dns>Dns</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+lan>Lan</A> -
  <A HREF=$SCRIPT_NAME?help+display+file+help+knelpunten>Knelpunten</A> -
  /
  <A HREF=$SCRIPT_NAME?help+update+file+$ETOOL+$EACTION>Update</A>
  "
fi
echo "</TD></TR></TABLE><CENTER>"

if [ "$HELPACTION" = "display" ]; then
  if [ "$HELPFILE" = "" ]; then
    echo "This tool is not defined in the helptool, sorry no help available."
  else

    # display the helpfile if present.
    if [ ! -f $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE ]; then
      echo "Sorry, currently no help defined ($HELPFILE)."
    else
      displayHelpfile $HELPFILE
    fi
  fi
elif [ "$HELPACTION" = "update" ]; then
  echo "<H2><FONT COLOR=red>Sorry working on this, no updates...<BR>
        will be replaced by a perl version.</FONT></H2>Working on $HELPFILE."
  if [ ! -f "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" ]; then
    echo "Creating a new helpfile.<P>
          <TABLE>
          <FORM method=\"post\" Action=$SCRIPT_NAME?help+addpage+file+$HELPFILE>
          <TR><TD>Title</TD><TD><INPUT NAME=\"title\" Size=60 Type=\"text\"></TD></TR>
          <TR><TD COLSPAN=2 ALIGN=center><INPUT TYPE=submit Value=\"Add helppage\">
          <INPUT TYPE=reset Value=\"CLEAR\"></TD></TR>
          </FORM>
          </TABLE>"
  else
    echo "<TABLE WIDTH=80%><TR><TD></TD><TD>"
    MARKER=""
    cat "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" | while read LINE
    do
      if [ "${LINE:0:11}" = "<A Name=end" ]; then
        MARKER=end
      elif [ "${LINE:0:8}" = "<A Name=" ]; then
        MARKER=${LINE:8:10}
      fi
      if [ "$MARKER" != "" ]; then
        echo "</TD></TR><TR><TD>
              <A HREF=$SCRIPT_NAME?help+insertText+$MARKER+$HELPFILE>Insert line before</A>
              <A HREF=$SCRIPT_NAME?help+updateText+$MARKER+$HELPFILE>Update</A>
              <A HREF=$SCRIPT_NAME?help+removeText+$MARKER+$HELPFILE>Remove</A><BR>
              </TD><TD>"
        MARKER=""
      fi
      echo "$LINE"
      if [ "$MARKER" = "end" ]; then
        echo "<CENTER>------------ End -----------"
      fi
    done
    echo "</TD></TR></TABLE>"
  fi
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"
  
elif [ "$HELPACTION" = "insertText" ]; then
  echo "Insert text.<BR>
  <TABLE>
  <FORM method=\"post\" Action=$SCRIPT_NAME?help+doInsertText+$HELPPARAM+$HELPFILE>
  <TR><TD>Highlight</TD><TD><INPUT NAME=\"boldtext\" Size=60 Type=\"text\"></TD></TR>
  <TR><TD>Text</TD><TD><TEXTAREA NAME=\"helpinfo\" ROWS=12 COLS=60></TEXTAREA></TD></TR>
  <TR><TD COLSPAN=2 ALIGN=center><INPUT TYPE=submit Value=\"Add helppage\">
  <INPUT TYPE=reset Value=\"CLEAR\"></TD></TR>
  </FORM>
  </TABLE>"
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"
elif [ "$HELPACTION" = "updateText" ]; then
  CONTEXT=no
  PARAMLEN=`expr 8 + ${#HELPPARAM}`
  cat "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" | while read LINE
  do
    if [ "${LINE:0:$PARAMLEN}" = "<A Name=$HELPPARAM" ]; then
      CONTEXT=begin
    elif [ "$CONTEXT" = "begin" ]; then
      #echo "$CONTEXT $LINE"
      HIGHLIGHT="$LINE"
      CONTEXT=text
      #echo "$CONTEXT $LINE"
    elif [ "$CONTEXT" = "text" ]; then
      #echo "$CONTEXT $LINE"
      DESCRIPTION="$LINE"
      CONTEXT=no
      printUpdateForm "$HIGHLIGHT" "$DESCRIPTION"
    fi
  done
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"
elif [ "$HELPACTION" = "removeText" ]; then
  if [ "$HELPPARAM" != "" ]; then
    TMPFILE="$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE.$$"
    PARAMLEN=`expr 8 + ${#HELPPARAM}`
    SKIP=no
    cat "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" | while read LINE
    do
      if [ "${LINE:0:$PARAMLEN}" = "<A Name=$HELPPARAM" ]; then
        SKIP=yes
      elif [ "${LINE:0:8}" = "<A Name=" ]; then
        SKIP=no
      fi
      if [ "$SKIP" = "no" ]; then
        echo "${LINE}" >> $TMPFILE
      fi
    done
    mv $TMPFILE $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE
  fi
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"

elif [ "$HELPACTION" = "doInsertText" ]; then
  if [ "$FORM_boldtext" = "" ]; then
    echo "No highlight defined."
  elif [ "$FORM_helpinfo" = "" ]; then
    echo "No helptext defined."
  else
    TMPFILE="$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE.$$"
    PARAMLEN=`expr 8 + ${#HELPPARAM}`
    cat "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" | while read LINE
    do
      if [ "${LINE:0:$PARAMLEN}" = "<A Name=$HELPPARAM" ]; then
        echo "<A Name=`date +%s`></A>
	      <B>$FORM_boldtext</B><BR>
	      $FORM_helpinfo
	      <P>" >> $TMPFILE
      fi
      echo "${LINE}" >> $TMPFILE
    done
    mv $TMPFILE $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE
  fi
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"

elif [ "$HELPACTION" = "doUpdateText" ]; then
  if [ "$FORM_boldtext" = "" ]; then
    echo "No highlight defined."
  elif [ "$FORM_helpinfo" = "" ]; then
    echo "No helptext defined."
  else
    TMPFILE="$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE.$$"
    PARAMLEN=`expr 8 + ${#HELPPARAM}`
    SKIP=no
    cat "$SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE" | while read LINE
    do
      if [ "${LINE:0:$PARAMLEN}" = "<A Name=$HELPPARAM" ]; then
        echo "<A Name=`date +%s`></A>
              <B>$FORM_boldtext</B><BR>
              $FORM_helpinfo
	      <P>" >> $TMPFILE
	SKIP=yes
      elif [ "${LINE:0:8}" = "<A Name=" ]; then
        SKIP=no
      fi
      if [ "$SKIP" = "no" ]; then
        echo "${LINE}" >> $TMPFILE
      fi
    done
    mv $TMPFILE $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE
  fi
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"
elif [ "$HELPACTION" = "addpage" ]; then
  if [ "$FORM_title" = "" ]; then
    echo "No title defined."
  else
    if [ ! -d "$SIM_HELPDIR/$SIM_HELPLANG" ]; then
      echo "No helpdir present, make sure it exists."
      mkdir -p $SIM_HELPDIR/$SIM_HELPLANG
    fi
    echo "Updating $HELPFILE."
    echo "<CENTER><H3>
          $FORM_title
	  </H3></CENTER>
          <A Name=end></A>" > $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE
    if [ ! -f $SIM_HELPDIR/$SIM_HELPLANG/$HELPFILE ]; then
      echo "Oops, no helpfile created, check the access rights on the helpdir."
    else
      displayHelpfile $HELPFILE
    fi
  fi
  echo "<P><A HREF=$SCRIPT_NAME?help+display+file+$HELPFILE>Display help</A>"
else
  echo "Oops, action not defined......."
fi

# ---------- end ----------
